﻿/*!
* File Name: featured.js
* Created When:     2012-03-31
* Created by:       陈鹏
* Created Whys:     特色活动列表页引用此JS
*/
/*全局变量*/
var topHtml = $('#pageTop');
var bottomHtml = $('#pageBottom');
var mainHtml = $('#pageMain');
var preName;
var nextName;
var dirNames;
if (g_langID == 1033) {
    preName = 'previous';
    nextName = 'next';
    dirNames = 'en';
}
else {
    preName = '上一页';
    nextName = '下一页';
    dirNames = 'cn';
}
$(document).ready(function () {
    ReqAjaxData(0, 6);
});
/*公共方法*/
/*CommonFunctionBegin*/
//转换时间格式
function renderTime(data) {
    if (data == null || typeof data == "undefined") return "";
    var date = new Date(parseInt(data.replace("/Date(", "").replace(")/", ""), 10));
    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    var s = currentDate + "/" + month + "/" + date.getFullYear();
    if (date.getHours() == 0 && date.getMinutes() == 0)
        return s;
    else
        return s + " " + date.getHours() + ":" + date.getMinutes();
}
/*CommonFunctionEnd*/

//上一页
function prePaging(pageIndex, pageSize) {
    if (pageIndex <= 1) {
        ReqAjaxData(0, pageSize);
    }
    else {
        ReqAjaxData((pageIndex - 1), pageSize);
    }

}
//下一页
function nextPaging(pageIndex, pageSize, pageNum) {

    if ((pageIndex + 1) >= pageNum) {
        //ReqAjaxData((pageNum-1), pageSize,eID);
    }
    else {
        ReqAjaxData((pageIndex + 1), pageSize);
    }
}

//显示分页页脚
function showFoot(total, pageSize, pageIndex) {
    var pageNum;
    if (total % pageSize == 0) {
        pageNum = Math.floor(total / pageSize);
    }
    else {
        pageNum = Math.floor(total / pageSize) + 1;
    }
    if (pageNum > 0) {
        var str = '<li><a href="javascript:void(0);" style="cursor:hand" onclick="javascript:prePaging(' + pageIndex + ',' + pageSize + ');">&lt; ' + preName + '</a></li>';
        str += '<li><span>' + (pageIndex + 1) + '/' + pageNum + '</span></li>';
        str += '<li><a href="javascript:void(0);" style="cursor:hand" onclick="javascript:nextPaging(' + (pageIndex) + ',' + pageSize + ',' + pageNum + ');">' + nextName + ' &gt;</a></li>';
        $('.spage').html(str);
        //topHtml.append(str);
        //bottomHtml.append(str);
    }
}

//输出listHtml
function renderHtml(model, num) {
    var topImg = '/uploadfiles/image/' + model.TopImg; //首页置顶图
    var classCss = model.ArtForm.Css;
    var title = model.Title;
    var fullShortDesc = model.ShortDescription;
    var shortDesc = fullShortDesc;
    var cutLength = (g_langID == 2052) ? 258 : 500;
    if (shortDesc.length > cutLength)
        shortDesc = shortDesc.substring(0, cutLength) + "...";
    //var city=model.City;
    var city = g_langID == 1033 ? model.CityModel.Enname : model.CityModel.Cnname;
    var arts = model.ArtForm.Name;
    var commentsNum = model.CommentsNum;
    var photosNum = model.PhotosNum;
    var videosNum = model.VideosNum;
    var errorImg = '/uploadfiles/defaultImg/featured_def.jpg';
    if (model.TopImg == null || model.TopImg == "") {
        topImg = errorImg;
    }
    var a_Link = model.HtmlFileName;
    if (model.HtmlFileName == "" || model.HtmlFileName == null) {
        a_Link = model.ID + ".html";
    }
    var strComment = g_langID == 1033 ? 'comments with' : '条评论';
    var strVideo = g_langID == 1033 ? 'videos &amp;' : '个视频';
    var strPhoto = g_langID == 1033 ? 'photos' : '张图片';
    var strBuild = '';
    strBuild += '<div class="featured' + classCss + '">';
    strBuild += '	 <a href="/' + dirNames + '/events/' + a_Link + '"><img   src="' + topImg + '" alt=""></a>';
    strBuild += '    <dl>';
    strBuild += '        <dt>0' + num + '</dt>';
    strBuild += '        <dd><img src="/' + dirNames + '/images/icon_medal_' + classCss + '_l.png" alt="" class="png"></dd>';
    strBuild += '    </dl>';
    strBuild += '    <p><a href="/' + dirNames + '/events/' + a_Link + '" title="' + fullShortDesc + '"><span>' + title + '</span>' + shortDesc + '</a></p>';
    strBuild += '    <div class="moreinfo">';
    strBuild += '        <ul>';
    strBuild += '           <li><address>' + city + ' / ' + arts + '</address></li>';
    strBuild += '           <li><span>' + commentsNum + '</span> ' + strComment + ' <span>' + videosNum + '</span> ' + strVideo + ' <span>' + photosNum + '</span> ' + strPhoto + '</li>';
    strBuild += '        </ul>';
    strBuild += '        <div class="detail"><a href="/' + dirNames + '/events/' + a_Link + '"><img src="/' + dirNames + '/images/btn_detail.gif" alt="Detail"></a></div>';
    strBuild += '     </div>';
    strBuild += '     <div class="eventsbtm"><img src="/' + dirNames + '/images/featured_events_line_' + classCss + '.jpg" alt=""></div>';
    strBuild += '   </div>';
    //alert(strBuild);

    $('.endinfo').before(strBuild);
}

//显示list
function showList(pagingList, pageIndex, pageSize) {
    topHtml.html("");
    mainHtml.html("");
    bottomHtml.html("");
    $('.title').siblings("div:not(.endinfo)").remove();
    var total = pagingList.Capacity; //总数
    if (total > 0) {
        var length = pagingList.List.length;
        for (var i = 0; i < length; i++) {
            renderHtml(pagingList.List[i], (i + 1));
        }
        if (total > pageSize) {
            showFoot(total, pageSize, pageIndex);
        }
    }
}

//ajax请求数据
function ReqAjaxData(pageIndex, pageSize) {
    var url = '/GateService/AjaxHttpGate.aspx?cmd=ReqFeatured&langID=' + g_langID + '&pageIndex=' + pageIndex + '&pageSize=' + pageSize + '&random=' + new Date().toLocaleString();

    jQuery.ajax({
        url: url,
        type: 'get',
        async: false,
        success: function (data) {
            showList(data, pageIndex, pageSize);
        },
        dataType: 'json'
    });
}
